package com.woniuxy.carlife.carlifeuserweb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.carlife.carlifecommonsweb.exception.CarlifeGlobalException;
import com.woniuxy.carlife.carlifecommonsweb.util.CopyUtil;
import com.woniuxy.carlife.carlifeusermodel.dto.ShippingaddressDto;
import com.woniuxy.carlife.carlifeuserweb.entity.Shippingaddress;
import com.woniuxy.carlife.carlifeuserweb.mapper.ShippingaddressMapper;
import com.woniuxy.carlife.carlifeuserweb.mapper.ShoppingcartMapper;
import com.woniuxy.carlife.carlifeuserweb.service.ShippingaddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.awt.*;
import java.util.List;

/**
 * @author zjq
 * @ClassName:ShippingaddressServiceImpl
 * @Description:收货地址
 * @date 2021/4/6 20:02
 **/

@Service
public class ShippingaddressServiceImpl extends ServiceImpl<ShippingaddressMapper, Shippingaddress> implements ShippingaddressService {

    @Resource
    ShippingaddressMapper shippingaddressMapper;



    /*
     * 功能描述〈根据用户查询所有的收货地址〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 16:08
     * @throws
     */

    @Override
    public List<Shippingaddress> findShippingaddressByUserId(Integer userId) {
        QueryWrapper<Shippingaddress> shippingaddressQueryWrapper = new QueryWrapper<>();
        shippingaddressQueryWrapper.eq("user_id", userId);
        List<Shippingaddress> shippingaddresses = shippingaddressMapper.selectList(shippingaddressQueryWrapper);

        return shippingaddresses == null ? null : shippingaddresses;
    }


    /*
     * 功能描述〈根据用户id查询默认收货地址〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 16:01
     * @throws
     */

    @Override
    public Shippingaddress findShippingaddressDefaultByUserId(Integer userId) {
        QueryWrapper<Shippingaddress> shippingaddressQueryWrapper = new QueryWrapper<>();
        shippingaddressQueryWrapper.eq("user_id", userId).eq("shippingaddress_default", 1);
        Shippingaddress shippingaddress = shippingaddressMapper.selectOne(shippingaddressQueryWrapper);

        return shippingaddress == null ? null : shippingaddress;
    }


    /*
     * 功能描述〈修改地址〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/13 5:35
     * @throws
     */

    @Override
    public boolean modifyShippingaddressByshippingaddressId(ShippingaddressDto shippingaddressDto) {
        Shippingaddress one = CopyUtil.getOne(shippingaddressDto, Shippingaddress.class);
        if (one.getShippingaddressDefault() == 1) {
            Boolean aBoolean = modifyShippingaddressDefaultByUserId(one.getUserId(), one.getShippingaddressDefault());
            if (!aBoolean) {
                return false;
            }
        }
        QueryWrapper<Shippingaddress> shippingaddressQueryWrapper = new QueryWrapper<>();
        shippingaddressQueryWrapper.eq("shippingaddress_id", one.getShippingaddressId());
        int i = shippingaddressMapper.update(one, shippingaddressQueryWrapper);

        return i > 0 ? true : false;
    }

    /*
     * 功能描述〈删除地址〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/13 8:53
     * @throws
     */
    @Override
    public boolean deleteShippingaddressByshippingaddressId(Integer shippingaddressId) {

        int i = shippingaddressMapper.deleteById(shippingaddressId);
        return i > 0 ? true : false;
    }



    /*
     * 功能描述〈新增地址〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/14 20:54
     * @throws
     */

    @Override
    public Boolean addShippingaddress(Shippingaddress shippingaddress) {
        if (shippingaddress.getShippingaddressDefault() == 1) {
            List<Shippingaddress> shippingaddressByUserId = findShippingaddressByUserId(shippingaddress.getUserId());

            if (!shippingaddressByUserId.isEmpty()) {


                Boolean aBoolean = modifyShippingaddressDefaultByUserId(shippingaddress.getUserId(), shippingaddress.getShippingaddressDefault());
                if (!aBoolean) {
                    return false;
                }
            }

        }

        int insert = shippingaddressMapper.insert(shippingaddress);
        return insert > 0 ? true : false;
    }

    /*
     * 功能描述〈修改掉其他地址默认地址只能有一个〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/14 23:00
     * @throws
     */
    @Override
    public Boolean modifyShippingaddressDefaultByUserId(Integer userId, Integer shippingaddressDefault) {
        Boolean aBoolean = shippingaddressMapper.modifyShippingaddressDefaultByUserId(userId, shippingaddressDefault);

        return aBoolean ? true : false;


    }

}
